package com.bw.shopping.mvvm.viewmodel;

import androidx.databinding.ObservableField;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bw.mvvm.entity.BaseEntity;
import com.bw.mvvm.entity.SeckillEntity;
import com.bw.mvvm.fleld.UIField;
import com.bw.mvvm.mvvm.viewmodel.BaseViewModel;
import com.bw.mvvm.utils.SpUtils;
import com.bw.my.mvvm.user.LoginActivity;
import com.bw.shopping.adapter.SeckillAdapter;
import com.bw.shopping.mvvm.model.SeckillParticulaesModel;
import com.bw.shopping.mvvm.model.entity.SeckillAction;
import com.chad.library.adapter.base.BaseQuickAdapter;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import okhttp3.RequestBody;

public class SeckillParticulaesViewModel extends BaseViewModel<SeckillParticulaesModel> {

    public String value;
    public String image;
    public String money;
    public BaseQuickAdapter adapter = new SeckillAdapter();
    public ObservableField<String> timeNow = new ObservableField<>();
    public SeckillEntity.DataBean seckillData;
    @Override
    protected void result(BaseEntity entity) {
        if (entity.getMessage().equals("添加购物车成功")){
            ToastUtils.showShort(entity.getMessage());
            EventBus.getDefault().postSticky(100);
        }
    }
    @Subscribe(sticky = true)
    public void getEventBus(SeckillEntity.DataBean dataBean){
        seckillData = dataBean;
        if (dataBean != null){
            value = dataBean.getGoods_default_sku();
            money = "秒杀价:￥"+dataBean.getGoods_default_price();
            image = dataBean.getGoods_default_icon();
        }
    }
    HashMap<String,Object> map = new HashMap<>();
    public void requestScekill(long nowTime){
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long mytime = nowTime*1000;
        String dateString = formatter.format(mytime);
        timeNow.set(dateString);
        LogUtils.e(nowTime);
        map.put("time",nowTime);

        RequestBody body = m.createBody(map);
        m.entityObservable(body).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this);
    }


    public void onJoin(){
        String user = SpUtils.getUser();
        if (user != null){
            map = new HashMap<>();
            map.put("goodsCount",seckillData.getGoods_num());
            map.put("goodsDesc",seckillData.getGoods_desc());
            map.put("goodsIcon",seckillData.getGoods_default_icon());
            map.put("goodsId",seckillData.getId());
            map.put("goodsPrice",seckillData.getGoods_default_price());
            map.put("goodsSku",seckillData.getGoods_default_sku());
            map.put("userId",453);
            SeckillAction action = new SeckillAction();
            action.action = "ScekillNowTime";
            EventBus.getDefault().postSticky(action);

        }else {
            ToastUtils.showShort("未登录 请登录");
            HashMap<String,Object> hashMap = new HashMap<>();
            hashMap.put(UIField.PAGECS, LoginActivity.class);
            startPage(hashMap);
        }


    }

    @Override
    public void onCreate() {
        super.onCreate();
        EventBus.getDefault().register(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
    public void onBack(){
        finishPage();
    }
}
